<script setup lang="js">
import {ref, onMounted} from 'vue'
import {Tickets} from "@element-plus/icons-vue";
import {
  fetchProductAttrCateList,
  createProductAttrCate,
  updateProductAttrCate,
  deleteProductAttrCate
} from '@/api/productAttrCate.js'

// 获取数据
onMounted(() => {
  getAttrData()
})
const attrData = ref([])
const getAttrData = async () => {
  const res = await fetchProductAttrCateList(listQuery)
  console.log(res.data.data.list)
  attrData.value = res.data.data.list
  total.value = res.data.data.total
}

const total = ref(0)
const productAttrCatForm = ref(null)
const listQuery = {
  pageNum: 1,
  pageSize: 10,
}
const productAttrCate = ref({
  id: null,
  name: null,
})
const dialogTitle = ref('')
const dialogVisible = ref(false)
const rules = ref({
  name: [
    { required: true, message: '请输入类型名称', trigger: 'blur' }
  ],
})

// TODO: 属性列表
const getAttrList = (row) => {
  console.log(row)
}

// TODO: 参数列表
const getParamList = (row) => {
  console.log(row)
}

// TODO: 添加属性列表
const handleAddAttr = () => {
  productAttrCate.value = {
    id: null,
    name: null,
  }
  dialogVisible.value = true
  dialogTitle.value = '添加类型'
}

// TODO: 编辑属性列表
const handleEditAttr = (row) => {
  dialogVisible.value = true
  dialogTitle.value = '编辑类型'
  productAttrCate.value = row
}

// TODO: 删除属性列表
const handleDeleteAttr = (row) => {
  console.log(row)
}

// TODO: 保存属性列表
const handleSave = (formName) => {
  // productAttrCatForm.value.validate((valid) => {
  //   if (valid) {
  //     if (productAttrCate.id) {
  //       updateProductAttrCate(productAttrCate.id, productAttrCate).then(() => {
  //         dialogVisible.value = false
  //         getAttrData()
  //       })
  //     } else {
  //       createProductAttrCate(productAttrCate).then(() => {
  //         dialogVisible.value = false
  //         getAttrData()
  //       })
  //     }
  //   } else {
  //     console.log('error submit!!')
  //     return false
  //   }
  // })
  console.log(formName)
}


// 页码改变
const handleCurrentChange = (currentPage) => {
  listQuery.pageNum = currentPage
  getAttrData()
}

// 页码大小改变
const handleSizeChange = (pageSize) => {
  listQuery.pageSize = pageSize
  listQuery.pageNum = 1
  getAttrData()
}
</script>

<template>
  <div class="product-attr">
    <div class="list-search">
      <div class="add-btn">
        <el-icon>
          <Tickets/>
        </el-icon>
        <span>数据列表</span>
      </div>
      <el-button type="primary" icon="plus" style="margin-left: 20px;" @click="handleAddAttr">
        添加商品
      </el-button>
    </div>
    <!-- 列表-->
    <div class="list-container">
      <el-table :data="attrData" border>
        <el-table-column label="编号" width="100" align="center">
          <template #default="{row}">{{ row.id }}</template>
        </el-table-column>
        <el-table-column label="类型名称" align="center">
          <template #default="{row}">{{ row.name }}</template>
        </el-table-column>
        <el-table-column label="属性数量" align="center">
          <template #default="{row}">{{ row.attributeCount == null ? 0 : row.attributeCount }}</template>
        </el-table-column>
        <el-table-column label="参考数量" align="center">
          <template #default="{row}">{{ row.paramCount == null ? 0 : row.paramCount }}</template>
        </el-table-column>
        <el-table-column label="设置" align="center">
          <template #default="{row}">
            <el-button type="text"
                @click="getAttrList(row)">属性列表
            </el-button>
            <el-button type="text"
                @click="getParamList(row)">参数列表
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="primary" icon="edit" @click="handleEditAttr(row)">编辑</el-button>
            <el-button type="danger" icon="delete" @click="handleDeleteAttr(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          background
          layout="total, sizes,prev, pager, next,jumper"
          :total="total"
          :page-sizes="[5, 10, 20, 30]"
          v-model:page-size="listQuery.pageSize"
          v-model:current-page="listQuery.pageNum"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 弹窗-->
    <el-dialog
        :title="dialogTitle"
        v-model="dialogVisible"
        width="25%"
        style="padding: 20px;"
    >
      <el-form ref="productAttrCatForm" :model="productAttrCate" :rules="rules" label-width="120px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="productAttrCate.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave('productAttrCatForm')">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<style scoped lang="scss">
.product-attr {
  margin-top: 20px;
  display: flex;
  flex-direction: column;

  .list-search {
    //height: 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 20px;
    margin-bottom: 20px;
    border: 1px solid #d5d5d5;
    border-radius: 5px;

    .add-btn {
      display: flex;
      align-items: center;

      span {
        margin-left: 8px;
        font-size: 18px;
      }
    }
  }

  .pagination {
    margin-top: 20px;
  }
}

</style>